﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BlockControll : MonoBehaviour
{
    //设置障碍物中间间距大小，以及障碍物的高度
    public float space = 10;
    public float yPosition = 0;

    [SerializeField]
    public RectTransform blockUp;

    [SerializeField]
    public RectTransform blockDown;

    //记录当前障碍物是否已经被越过，用来防止重复计算单个障碍物的分数
    public bool crossed = false;

    // Start is called before the first frame update
    void Start()
    {
        RectTransform parentCanvas = GetComponentInParent<RectTransform>();
        float localScreenHeight = Screen.height / parentCanvas.lossyScale.y;

        //初始化上障碍物的位置与大小
        blockUp = transform.GetChild(0).transform as RectTransform;
        float blockUpHeight = localScreenHeight / 2 - (yPosition + space / 2);
        blockUpHeight = blockUpHeight < 0 ? 0 : blockUpHeight;

        float blockUpYPosition = yPosition + space / 2 + blockUpHeight / 2;

        blockUp.sizeDelta = new Vector2(blockUp.sizeDelta.x, blockUpHeight);
        blockUp.anchoredPosition = new Vector2(blockUp.anchoredPosition.x, blockUpYPosition);

        //初始化下障碍物的位置与大小
        blockDown = transform.GetChild(1).transform as RectTransform;

        float blockDownHeight = localScreenHeight - blockUpHeight - space;
        blockDownHeight = blockDownHeight < 0 ? 0 : blockDownHeight;

        float blockDownYPosition = yPosition - blockDownHeight/2 - space/2;

        blockDown.sizeDelta = new Vector2(blockDown.sizeDelta.x, blockDownHeight);
        blockDown.anchoredPosition = new Vector2(blockDown.anchoredPosition.x, blockDownYPosition);
    }

    // Update is called once per frame
    void Update()
    {

    }
}
